import java.util.ArrayList;

import tools.Parser;

import algorithms.OthelloAlgorithm;
import algorithms.OthelloEvaluatorCountPieces;
import algorithms.OthelloEvaluatorMobility;
import exception.IllegalMoveException;
import game.AbstractOthelloPosition;
import game.OthelloAction;
import game.OthelloPosition;


public final class Othello {
	private static int[] TIME = {5, 10, 15, 30};
	private static int[] DEPTHS = {5, 7, 7, 7};
	private static int DEPTH = 7;
//	private static boolean IS_STARTING = false;
//	public static boolean player1Pass = false;
//	public static boolean player2Pass = false;
//	
//	private static boolean WHITE = true; 

	
	public static void main(String args[]) throws IllegalMoveException {		
		AbstractOthelloPosition othello = Parser.parseArgs(args[0]);
		int time = Integer.parseInt(args[1]);
		
		DEPTH = DEPTHS[DEPTHS.length - 1];
		for(int i = 0; i < TIME.length; i++ ) {
			if(time  < TIME[i]){
				DEPTH = DEPTHS[i];
				break;
			}
		
		}
		othello.illustrate();
		OthelloAlgorithm engine  = new OthelloAlgorithm(DEPTH, new OthelloEvaluatorMobility(othello.toMove()));
		OthelloAction action = engine.evaluate(othello);
		System.out.println(action.sendToMediator());
		
		
/*		
		AbstractOthelloPosition othello = new OthelloPosition();
		OthelloAlgorithm algo  = new OthelloAlgorithm(DEPTH, new OthelloEvaluatorCountPieces(!WHITE));
		
		OthelloAlgorithm algoWinner  = new OthelloAlgorithm(DEPTH, new OthelloEvaluatorMobility(WHITE));

		OthelloAction a;
		ArrayList<Double> times = new ArrayList<Double>();
		long start, end;
		if(!IS_STARTING)
			othello.changePlayer();
		while(!isEnd(othello)){
			if(othello.toMove()){ // WHITE
				start = System.currentTimeMillis();
				a = algoWinner.evaluate(othello);
				end = System.currentTimeMillis();
				times.add((end - start) / 1000.0);
				player1Pass = (a.isPassMove()) ? true : false;
			} else{ // BLACK
				a = algo.evaluate(othello);
				player2Pass = (a.isPassMove()) ? true : false;
			}
			System.out.println(a);
			othello.makeMove(a);
			othello.illustrate();
		}
		System.out.println("WINNER : " + othello.getWinner());
		System.err.println(times);
		System.err.println("Max time : " + java.util.Collections.max(times));
	}
	
	public static boolean isEnd(AbstractOthelloPosition p){
		if(player1Pass && player2Pass){
			return true;
		}else if(p.isOnlyBlack() || p.isOnlyWhite()){
			return true;
		}else{
			return false;
		}*/
	}

}
